//
//  HXDHomeViewController.m
//  qingDaoCircleOfLife
//
//  Created by huxuedong on 15/11/21.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

#import "HXDHomeViewController.h"
#import "HXDLoginViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "HXDLocationManager.h"
#import "HXDHomeDataModel.h"
#import "HXDFunction.h"
#import "AppDelegate.h"
#import "HXDPictureTableViewCell.h"
#import "HXDCircleView.h"
#import "HXDFamousTableViewCell.h"
#import "HXDGuessTableViewCell.h"
#import "HXDGoodsIntroViewController.h"
#import "HXDShopInfoTableViewController.h"

@interface HXDHomeViewController () <CLLocationManagerDelegate, UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) HXDPictureTableViewCell *headerView;
@property (strong, nonatomic) HXDHomeDataModel *homeData;

@end

@implementation HXDHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.leftButton.hidden = YES;
    [self loadData];
    //定位
    [HXDLocationManager localWithSuccess:^(CLLocationCoordinate2D coordinate2D) {
        NSLog(@"%f",coordinate2D.latitude);
    } orError:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableViewCell"];
    self.headerView = [HXDPictureTableViewCell pictureTableViewCell];
    self.headerView.frame = CGRectMake(0, 0, kUIScreenWidth, (425 - 88) * 0.5);
    self.tableView.tableHeaderView = self.headerView;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpController) name:@"jump" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orLogin:) name:@"isLogin" object:nil];
}

- (void)orLogin:(NSNotification *)notification {
    if (notification.object) {
        self.rightButton.frame = CGRectMake(kUIScreenWidth - 2 * kNavigationButtonWidth + 30, kUIStatusBarHeight, kNavigationButtonWidth, kNavigationButtonWidth);
        [self.rightButton setTitle:nil forState:UIControlStateNormal];
        UIImage *icon = [UIImage imageWithData:[kSaveData objectForKey:kIconImage]];
        [self.rightButton setBackgroundImage:icon forState:UIControlStateNormal];
        self.rightButton.layer.cornerRadius = 20;
        self.rightButton.clipsToBounds = YES;
        self.rightButton.userInteractionEnabled = NO;
    }
}

- (void)jumpController {
    HXDShopInfoTableViewController *shopInfoTableViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"HXDShopInfoTableViewController"];
    [self.navigationController pushViewController:shopInfoTableViewController animated:YES];
}

- (void)loadData {
    //创建参数字典
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    [parameter setValue:@"120.38" forKey:@"lon"];
    [parameter setValue:@"36.06" forKey:@"lat"];
    [parameter setValue:[HXDFunction getTime] forKey:@"time"];
    NSArray *temp = @[APP_ID,parameter[@"lon"],parameter[@"lat"],parameter[@"time"],APP_IDEntier];
    [parameter setObject:[HXDFunction MD5Striing:temp] forKey:@"sign"];
    [parameter setObject:APP_ID forKey:@"app_id"];
    //获取数据模型
    [HXDHTTPSessionManager getHomeDataWithParameter:parameter andWithSuccessBlock:^(id json) {
        HXDHomeDataModel *dataModel = (HXDHomeDataModel *)json;
        self.headerView.dataModel = dataModel;
        self.homeData = dataModel;
        //发送通知传递数据
        [[NSNotificationCenter defaultCenter] postNotificationName:@"data" object:dataModel];
    } andWithFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)enterMethod {
    HXDLoginViewController *loginViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"HXDLoginViewController"];
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    [app.tabBar isHiddenTabBar:YES];
    [self.navigationController pushViewController:loginViewController animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        return 1;
    }
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"tableViewCell";
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        //取消点击单元格效果
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        HXDCircleView *circleView = [HXDCircleView circleView];
        [cell addSubview:circleView];
        return cell;
    } else if (indexPath.section == 1) {
        HXDFamousTableViewCell *cell = [HXDFamousTableViewCell famousTableViewCell];
        return cell;
    } else {
        HXDGuessTableViewCell *cell = [HXDGuessTableViewCell guessTableViewCell];
        cell.guessData = self.homeData.home.guessList[indexPath.row];
        return cell;
    }
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 1) {
        return 200;
    } else {
        return 100;
    }
}

//设置headerView的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 60 * 0.5;
    }
}

//设置headerView的内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIImageView *imageView = [[UIImageView alloc] init];
    if (section == 1) {
        imageView.image = [UIImage imageNamed:@"ming"];
        return imageView;
    } else if (section == 2) {
        imageView.image = [UIImage imageNamed:@"cai"];
        return imageView;
    } else {
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HXDGoodsIntroViewController *goodsIntroViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"HXDGoodsIntroViewController"];
    [self.navigationController pushViewController:goodsIntroViewController animated:YES];
}

@end
